package cn.anecansaitin.firecrafting.client.crafting;

import cn.anecansaitin.firecrafting.ModConstant;
import cn.anecansaitin.firecrafting.api.client.crafting.IJEIRecipeRender;
import cn.anecansaitin.firecrafting.api.common.crafting.IFireRecipe;
import cn.anecansaitin.firecrafting.client.util.RenderHelper;
import cn.anecansaitin.firecrafting.common.crafting.FireEffectRecipe;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.network.chat.*;
import net.minecraft.world.effect.MobEffect;

import java.util.ArrayList;
import java.util.List;

public class FireEffectRecipeRender implements IJEIRecipeRender {
    public static final FireEffectRecipeRender INSTANCE = new FireEffectRecipeRender();
    private final MutableComponent describe = new TranslatableComponent("firecrafting.jei.effect.describe").withStyle(ChatFormatting.WHITE);
    private final MutableComponent radius = new TranslatableComponent("firecrafting.jei.effect.radius").withStyle(ChatFormatting.WHITE);
    private final int x = 140;
    private final int y = 34;
    private final int weight = 18;
    private final int height = 15;

    private FireEffectRecipeRender() {
    }

    @Override
    public void drawBeforeFire(IFireRecipe recipe, PoseStack stack, double mouseX, double mouseY) {
        //绑定渲染的贴图
        RenderSystem.setShaderTexture(0, ModConstant.JEI_GUI);
        //渲染
        GuiComponent.blit(stack, x, y, 189, 54, weight, height, 256, 256);
        //渲染文字
        GuiComponent.drawCenteredString(stack, Minecraft.getInstance().font, describe, 151, 55, 0x000000);
        GuiComponent.drawCenteredString(stack, Minecraft.getInstance().font, radius.copy().append(new TextComponent("" + ((FireEffectRecipe)recipe).getRadius())), 151, 65, 0x000000);
    }

    @Override
    public List<Component> getTooltip(IFireRecipe recipe, double mouseX, double mouseY) {
        ArrayList<Component> list = new ArrayList<>();
        if (RenderHelper.mouseOver(mouseX, mouseY, x, y, weight, height)) {
            ((FireEffectRecipe)recipe).getOutputs()
                    .forEach((e) -> {
                        MobEffect effect = e.getEffect();
                        list.add(((BaseComponent) effect.getDisplayName())
                                .withStyle(effect.isBeneficial() ? ChatFormatting.GOLD : ChatFormatting.RED)
                                .append(new TextComponent("" + e.getAmplifier()))
                                .append(new TextComponent(" " + e.getDuration() + "Tick")));
                    });
        }
        return list;
    }
}
